package tw.zld369504.wol.action;

import android.os.Bundle;
import android.os.Message;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Random;

import tw.zld369504.wol.R;
import tw.zld369504.wol.activity.MainActivity;
import tw.zld369504.wol.utils.Constant;
import tw.zld369504.wol.utils.PacketUtil;

/**
 * main activity controller
 */
public class MainActivityController implements MainActivityActionInterface {

    private int portNumber;
    private String macAddress;
    private String ipAddress;
    private MainActivity.MyHandler mHandler;
    MainActivity activity;
    private PacketUtil util;

    public MainActivityController(MainActivity activity) {
        this.activity = activity;
    }

    @Override
    public void sendMagicPacket() {
        activity.showProgressDialog();
        new Thread(sp).start();
    }

    private Runnable sp = new Runnable() {
        @Override
        public void run() {
            Message message = Message.obtain();
            message.setTarget(mHandler);
            try {
                DatagramSocket socket = new DatagramSocket(portNumber);
                byte[] mac = util.macStr2Byte(macAddress);
                socket.send(util.getConstructedPacket(mac, ipAddress));
                socket.close();
                message.what = Constant.STATE_SOCKET_OK.ordinal();
            } catch (SocketException e) {
                message.what = Constant.STATE_SOCKET_FAILURE.ordinal();
                message.obj = e.toString();
            } catch (IOException e) {
                message.what = Constant.STATE_IO_ERROR.ordinal();
                message.obj = e.toString();
            } catch (Exception e) {
                message.what = Constant.STATE_OTHER_ERROR.ordinal();
                message.obj = e.toString();
            } finally {
                message.sendToTarget();
            }
        }
    };

    @Override
    public boolean validateInputData(String ip, String port, String mac) {
        if (!util.checkIpValid(ip)) {
            mHandler.sendEmptyMessage(Constant.STATE_IP_ERROR.ordinal());
            return false;
        }
        if ((!util.checkMacAddressValid(mac))) {
            mHandler.sendEmptyMessage(Constant.STATE_MAC_ERROR.ordinal());
            return false;
        }
        try {
            int portN = Integer.parseInt(port);
            if (portN < 1 || portN > 65535) {
                Message msg=Message.obtain();
                msg.what=Constant.STATE_PORT_ERROR.ordinal();
                msg.setTarget(mHandler);
                msg.obj=activity.getString(R.string.invaild_port_range);
                msg.sendToTarget();
                return false;
            } else {
                ipAddress=ip;
                macAddress=mac;
                portNumber=portN;
                return true;
            }
        } catch (NumberFormatException e) {
            Random random = new Random();
            portNumber=random.nextInt(65535)+1;
            ipAddress=ip;
            macAddress=mac;
            return true;
        }
    }

    @Override
    public void onCreate(Bundle bundle) {
        mHandler = activity.getHandler();
        util = new PacketUtil();
    }

    @Override
    public void onDestroy() {
        activity = null;
        System.gc();
    }
}
